package com.huopaolan.yohuoshop.Mvp.View.Fragment;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;

import com.huopaolan.lib_core.Base.BaseFragment;
import com.huopaolan.yohuoshop.Adapter.StrollNewAdapter;
import com.huopaolan.yohuoshop.Bean.CommunityEntity;
import com.huopaolan.yohuoshop.Di.Compont.DaggerCommunityFragmentCompont;
import com.huopaolan.yohuoshop.Di.Modules.CommunityFragmentModules;
import com.huopaolan.yohuoshop.Listener.HomeBannerImageLoader;
import com.huopaolan.yohuoshop.Mvp.Contract.CommunityFragmentContract;
import com.huopaolan.yohuoshop.Mvp.Presenter.CommunityFragmentPresenter;
import com.huopaolan.yohuoshop.NetWork.API;
import com.huopaolan.yohuoshop.R;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class StrollNewFragment extends BaseFragment<CommunityFragmentPresenter> implements CommunityFragmentContract.CommunityIView, BaseRefreshListener {
    private StrollNewAdapter strollNewAdapter;
    private View headView;
    private Banner banner;
    private PullToRefreshLayout pullToRefreshLayout;
    private RecyclerView stroll_recycler;
    private List<CommunityEntity.Banner> bannerBeans = new ArrayList<>();
    private List<CommunityEntity.Brand> brandBeans = new ArrayList<>();
    private List<CommunityEntity.Values> valuesBeans = new ArrayList<>();

    @Override
    public View createView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_strollnew, null);
    }

    @Override
    public void onStart() {
        Window window = getActivity().getWindow();
        window.setStatusBarColor(Color.BLACK);
        super.onStart();
    }

    @Override
    public void initView(View view) {
        //初始化控件
        pullToRefreshLayout = view.findViewById(R.id.stroll_pull);
        pullToRefreshLayout.setRefreshListener(this);
        stroll_recycler = view.findViewById(R.id.stroll_recycler);
        headView = View.inflate(getActivity(), R.layout.item_stroll_banner, null);
        banner = headView.findViewById(R.id.stroll_banner);
        //添加适配器
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        strollNewAdapter = new StrollNewAdapter(R.layout.item_shop_message);
        stroll_recycler.setLayoutManager(layoutManager);
        strollNewAdapter.addHeaderView(headView, 0);
        stroll_recycler.setAdapter(strollNewAdapter);
    }

    @Override
    public void initData() {
        p.CommunityRequest();
    }

    @Override
    public void setUpDagger() {
        DaggerCommunityFragmentCompont.builder()
                .communityFragmentModules(new CommunityFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void CommunitySuccess(Map<String, List> map) {
        bannerBeans.addAll(map.get("bannerBeans"));
        valuesBeans.addAll(map.get("valuesBeans"));
        strollNewAdapter.addData(valuesBeans);
        List<String> stringList = new ArrayList<>();
        for (int i = 0; i < bannerBeans.size(); i++) {
            stringList.add(API.BaseUrl + bannerBeans.get(i).recommend_url);
        }
        banner.setImages(stringList);
        banner.isAutoPlay(true);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.setDelayTime(1 * 1500);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        banner.setImageLoader(new HomeBannerImageLoader());
        banner.start();
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }

    @Override
    public void refresh() {
        strollNewAdapter.notifyDataSetChanged();
        strollNewAdapter.getData().clear();
        p.CommunityRequest();
        pullToRefreshLayout.finishRefresh();
    }

    @Override
    public void loadMore() {
        pullToRefreshLayout.finishLoadMore();
    }
}
